#!/usr/bin/perl

# package de parsing dess document Rubis
package PrintingRules;
use Carp;
use Data::Dumper;

sub new {
	my($class)  = shift;
	my(%params) = @_;

	#print Dumper(\%params);

	my $self  = {
		'_string' => $params{'-rules'},
		'_rules'  => {}
	};
	
	bless ($self, $class);
	$self->_parse();

	return $self;
}


########################### PUBLIC ###############################################
sub _parse {
	my $self = shift;
	my @rules = split "\n", $self->{'_string'};
	#print Dumper(\@rules);
	my $rules = {};
	foreach (@rules) {
		my @param	= split ",", $_;
		my $user	= '';
		my $type	= '';
		foreach my $p (@param) {
			my ($key,$val) = split ":", $p;
			$key =~ s/(?:^\s+|\s+$)//g;
			$val =~ s/(?:^\s+|\s+$)//g;
			$key = lc $key;

			if ($key eq 'user') {
				$user = lc $val;
				$rules->{$user} = {};
			}
			if ($key eq 'type') {
				$type = lc $val;
				$rules->{$user}->{$type} = {'printer'=>'', 'copy'=>0, 'ged'=>0};
			}
			if ($key eq 'printer') {
				$rules->{$user}->{$type}->{'printer'} = $val;
			}
			if ($key eq 'copy') {
				$rules->{$user}->{$type}->{'copy'} = $val;
			}
			if ($key eq 'ged') {
				$rules->{$user}->{$type}->{'ged'} = $val;
			}
			
		}
	}
	$self->{'_rules'} = $rules;
}


sub get {
	my $self = shift;
	my ($user,$type) = (lc shift,lc shift);
	my $rules = $self->{'_rules'};

	if		(defined $rules->{$user}->{$type}) {
		return $rules->{$user}->{$type};
	} elsif (defined $rules->{'all'}->{$type}) {
		return $rules->{'all'}->{$type};
	} else {
		return undef;
	}
}

1;